//
//  FKCollectFilterView.m
//  GC_OC
//
//  Created by Macbook Pro on 2023/5/13.
//

#import "FKCollectFilterView.h"
#import "FKCollectFilterCell.h"
#import "FKCategory.h"

@interface FKCollectFilterView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UIView *cover;
@property (nonatomic, strong) NSArray *tables;
@property (nonatomic, strong) NSArray *datasList;

@end

@implementation FKCollectFilterView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.hidden = true;
    
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, -40, fkScreenW, 40)];
    [self addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor redColor];
    container.clipsToBounds = true;
    
    // 创建3个tableView
    NSMutableArray *tables = [NSMutableArray array];
    self.tables = tables;
    NSMutableArray *datasList = [NSMutableArray array];
    self.datasList = datasList;
    NSArray *widths = @[@(0.20), @(0.45), @(0.35)];
    NSArray *bgColors = @[[UIColor fk_colorWithHex:0xededed], [UIColor fk_colorWithHex:0xf6f6f6], [UIColor whiteColor]];
    CGFloat left = 0;
    for (NSInteger index = 0; index < 3; index++) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [container addSubview:tableView];
        [tables addObject:tableView];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = bgColors[index];
        
        CGFloat width = [widths[index] floatValue] * fkScreenW;
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(left);
            make.width.offset(width);
            make.top.bottom.offset(0);
        }];
        left += width;
        
        [datasList addObject:@[]];
    }
    
    UIView *cover = [[UIView alloc] init];
    [self addSubview:cover];
    self.cover = cover;
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.5;
    [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(updateShow)]];
    
    [cover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(container.mas_bottom);
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger index = [self.tables indexOfObject:tableView];
    NSArray *datas = self.datasList[index];
    return datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCollectFilterCell *cell = [FKCollectFilterCell cellWith:tableView];
    NSInteger index = [self.tables indexOfObject:tableView];
    NSArray *datas = self.datasList[index];
    cell.data = datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(40);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCollectFilterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setupSelectedState];
    
    NSInteger index = [self.tables indexOfObject:tableView];
    NSArray *datas = self.datasList[index];
    FKCategory *category = datas[indexPath.row];
    if (index == 0) { // 刷新第二级,然后清空第三级【如果没有下级，则直接选定最终结果】
        NSMutableArray *temp = [NSMutableArray arrayWithArray:self.datasList];
        NSMutableArray *nextDatas = [NSMutableArray array];
        FKCategory *firstModel = [FKCategory new];
        firstModel.name = @"不限";
        firstModel.id = @"";
        if (category.children.count > 0) {
            [nextDatas addObject:firstModel];
            [nextDatas addObjectsFromArray:category.children];
        } else {
            if (self.completeBlock) self.completeBlock(category);
//            [self setShow:false];
        }
        temp[index + 1] = nextDatas;
        temp[index + 2] = @[];
        self.datasList = temp;
        
        UITableView *nextTable = self.tables[index + 1];
        [nextTable reloadData];
        UITableView *lastTable = self.tables[index + 2];
        [lastTable reloadData];
    } else if (index == 1) { // 刷新第三级【如果没有下级，则直接选定最终结果】
        NSMutableArray *temp = [NSMutableArray arrayWithArray:self.datasList];
        NSMutableArray *nextDatas = [NSMutableArray array];
        FKCategory *firstModel = [FKCategory new];
        firstModel.name = @"不限";
        firstModel.id = @"";
        if (category.grandson.count > 0) {
            [nextDatas addObject:firstModel];
            [nextDatas addObjectsFromArray:category.grandson];
        } else {
            if (self.completeBlock) self.completeBlock(category);
//            [self setShow:false];
        }
        temp[index + 1] = nextDatas;
        self.datasList = temp;
        
        UITableView *nextTable = self.tables[index + 1];
        [nextTable reloadData];
    } else if (index == 2) { // 选定最终结果
        if (self.completeBlock) self.completeBlock(category);
//        [self setShow:false];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCollectFilterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setupSelectedState];
}

#pragma mark - private
- (void)updateShow
{
    if (self.cancelBlock) self.cancelBlock();
}

- (void)setShow:(BOOL)show
{
    _show = show;
    
    if (show) { // 显示
        self.hidden = false;
        [UIView animateWithDuration:0.25 animations:^{
            self.container.fk_top = 0;
        } completion:^(BOOL finished) {
            self.cover.hidden = false;
        }];
    } else { // 隐藏
        self.cover.hidden = true;
        [UIView animateWithDuration:0.25 animations:^{
            self.container.fk_top = -self.container.fk_height;
        } completion:^(BOOL finished) {
            self.hidden = true;
        }];
    }
}

- (void)setDatas:(NSArray *)datas
{
    if (datas) {
        NSMutableArray *temp = [NSMutableArray arrayWithArray:self.datasList];
        NSMutableArray *firstDatas = [NSMutableArray array];
        FKCategory *firstModle = [FKCategory new];
        firstModle.name = @"全部";
        firstModle.id = @"";
        [firstDatas addObject:firstModle];
        [firstDatas addObjectsFromArray:datas];
        temp[0] = firstDatas;
        self.datasList = temp;
        
        UITableView *firstTable = self.tables[0];
        [firstTable reloadData];
        
        // 在这里重新设置container的高度
        self.container.fk_height = fk_adjustW(40) * MIN(20, datas.count + 1);
        self.container.fk_top = - self.container.fk_height;
    }
}

@end
